<?php 
/**
 * 直通车<strong>客户端</strong>服务Action控制器基类
 */
class ClientAction extends Action {
	/** @var ApiModel */
	protected $_apiMdl = null;
	/** @var CategoryModel */
	protected $_cateMdl = null;
	/** @var WordModel */
	protected $_wordMdl = null;
	

	/**
	 * 关键词维护专用LOG
	 * 
	 * @param string $filename
	 * @param string $errMsg
	 * @param array(string) $words
	 */
	protected function logErr($filename, $errMsg, $words) {
		$f = fopen($filename, 'a');
		if(!empty($words)) {
			fwrite($f, '#WORDS: ' . join(',', $words) . PHP_EOL);
		}
		fwrite($f, "#ERROR @ " . date('Y-m-d H:i:s') . " - $errMsg" . PHP_EOL);
		fclose($f);
	
		// TODO send email
	
	}

	/**
	 * 获取ApiModel。防止创建多个实例。
	 * @return ApiModel 实例化过的ApiModel。
	 */
	protected function _getApiMdl() {
		if($this->_apiMdl === null) {
			$this->_apiMdl = new ApiModel;
		}
		return $this->_apiMdl;
	}
	
	/**
	 * 获取CategoryModel。防止创建多个实例。
	 * @return CategoryModel 实例化过的CategoryModel。
	 */
	protected function _getCateMdl() {
		if($this->_cateMdl === null) {
			$this->_cateMdl = new CategoryModel;
		}
		return $this->_cateMdl;
	}
	
	/**
	 * 获取WordModel。防止创建多个实例。
	 * @return WordModel 实例化过的WordModel。
	 */
	protected function _getWordMdl() {
		if($this->_wordMdl === null) {
			$this->_wordMdl = new WordModel;
		}
		return $this->_wordMdl;
	}
	
	public function display_json($r , $code = 200){
		$dom = $this->_get('dom');
		echo json_encode(array(
			'status' => $code,
			'data' => $r,
			'dom' => $dom
		));
        exit;
	}

	/**
	 * Log to global klogger defined in Scripts/common.php with level KLogger::Debug
	 * @param string $line Log message.
	 */
	protected function logDebug($line) {
		$this->_getKlog()->logDebug($line);
	}
	/**
	 * Log to global klogger defined in Scripts/common.php with level KLogger::Info
	 * @param string $line Log message.
	 */
	protected function logInfo($line) {
		$this->_getKlog()->logInfo($line);
	}
	/**
	 * Log to global klogger defined in Scripts/common.php with level KLogger::Warn
	 * @param string $line Log message.
	 */
	protected function logWarn($line) {
		$this->_getKlog()->logWarn($line);
	}
	/**
	 * Log to global klogger defined in Scripts/common.php with level KLogger::Error
	 * @param string $line Log message.
	 */
	protected function logError($line) {
		$this->_getKlog()->logError($line);
	}
	/**
	 * Log to global klogger defined in Scripts/common.php with level KLogger::Error
	 * @param Exception $e Exception.
	 * @param bool $isLogTrace Whether also log the detailed trace.
	 */
	protected function logException($e, $isLogTrace = true) {
		$this->_getKlog()->logError('捕获到错误: ' . $e->getMessage());
		if($isLogTrace) 
			$this->_getKlog()->logError('Trace:' . PHP_EOL . $e->getTraceAsString());
	}
	
	protected function _getKlog() {
		global $klog;
		if(!$klog) {
			import('@.Library.KLogger');
			$logLevel = ((defined('APP_DEBUG') && APP_DEBUG) || (isset($_GET['test']) && $_GET['test'] == 1)) 
					? KLogger::DEBUG 
				 	: defined('KLOG_LEVEL') ? KLOG_LEVEL : KLogger::INFO;
			$klog = new KLogger(SCRIPT_LOG_DIR . MODULE_NAME . 'Action-' . ACTION_NAME, $logLevel);
		}
		return $klog;
	}
	
}